package com.leancloud.circle.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.aries.ui.view.radius.RadiusEditText;
import com.aries.ui.view.radius.RadiusTextView;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.circle.LoginCallUtil;
import com.leancloud.circle.R;
import com.leancloud.circle.WindowsUtil;
import com.leancloud.circle.adapter.DiscussAdapter;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.event.CircleEvent;
import com.leancloud.circle.event.CommitEvent;
import com.leancloud.circle.manage.CircleBack;
import com.leancloud.circle.manage.CircleGlobal;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.event.inner.ThreadMode;

import java.util.List;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.base.BaseTitleFragment;
import tech.com.commoncore.basecomponent.ServiceFactory;

/**
 * 话题界面
 */
public class TopicFragment extends BaseTitleFragment implements View.OnClickListener{
    private SmartRefreshLayout smartRefresh;
    private RecyclerView recyclerView;
    private DiscussAdapter discussAdapter;

    private List<CircleBean> circles;   //圈子列表

    private FrameLayout commentInputLt;
    private RadiusEditText commentEt;
    private RadiusTextView commentSubmitTx;
    private InputMethodManager inputMethodManager;
    public static TopicFragment newInstance() {
        TopicFragment fragment = new TopicFragment();
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_topic;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        smartRefresh = mContentView.findViewById(R.id.smart_refresh_layout);
        recyclerView = mContentView.findViewById(R.id.circle_recycler_view);

        commentInputLt = mContentView.findViewById(R.id.comment_input_layout);
        commentEt = mContentView.findViewById(R.id.comment_input_content);
        commentSubmitTx = mContentView.findViewById(R.id.comment_input_submit);
        inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        discussAdapter = new DiscussAdapter(R.layout.circle_layout_item_topic);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(discussAdapter);


        commentSubmitTx.setOnClickListener(this);
        mContentView.findViewById(R.id.circle_publish_img).setOnClickListener(this);

        adaptiveKeyboardPushUp();

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestAllData();
            }
        });

        if (circles == null || circles.isEmpty()) {
            smartRefresh.autoRefresh();
        }

    }

    private void adaptiveKeyboardPushUp() {
        View contentView = mContentView.findViewById(R.id.circle_content_view);
        ViewTreeObserver.OnGlobalLayoutListener listener = WindowsUtil.getGlobalLayoutListener(commentEt, contentView, mContext, commentInputLt);
        commentEt.getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.comment_input_submit) {
            LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
                @Override
                protected void passBack() {

                }
            });
        } else if (v.getId() == R.id.circle_publish_img) {

            if (ServiceFactory.getInstance().getLoginService().getCurrentUser() != null){
                ARouter.getInstance().build("/topic/post").navigation();
            }else {
                ARouter.getInstance().build("/login/login").navigation();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_THREAD)
    public void event(CircleEvent event) {
        smartRefresh.autoRefresh();
    }

    private void requestAllData() {
        CircleGlobal.getCircleImp().requestCircle(1, 10, null, CircleGlobal.FUND_TOPIC, -1, 0, null, null, new CircleBack<List<CircleBean>>() {
            @Override
            public void onBack(List<CircleBean> beans, String err) {
                smartRefresh.finishRefresh();
                if (err == null && beans != null) {
                    circles = beans;
                }
                refreshCircleView(circles);
            }
        });
    }

    private void refreshCircleView(List<CircleBean> circles) {
        if (circles != null && !circles.isEmpty()) {
            discussAdapter.replaceData(circles);
            discussAdapter.notifyDataSetChanged();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_THREAD)
    public void commitEvent(CommitEvent event) {
        commentInputLt.setVisibility(View.VISIBLE);
        commentEt.setFocusable(true);
        commentEt.setFocusableInTouchMode(true);
        commentEt.requestFocus();
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.circle_topic_title)).setBackground(getResources().getDrawable(R.drawable.bg_titlebar_jb));
    }
}
